/*
 * @Description:londing 加载场景
 * @Author: maskMan
 * @LastEditTime: 2024-03-27 20:05:40
 */

import { Component, director, EditBox, Label, macro, VideoPlayer, view, _decorator } from 'cc'
import ResManager from '../general/resManager'
/**预加载游戏场景 */
const { ccclass, property } = _decorator
@ccclass
export class loading extends Component {
  @property(Label)
  tips: Label = null
  @property(Label)
  percentNum: Label = null
  @property(VideoPlayer)
  mp4: VideoPlayer = null
  @property(EditBox)
  account_edit: EditBox = null
  @property(EditBox)
  password_edit: EditBox = null
  @property(String)
  loadSceneName = 'hall'
  onLoad() {
    // CacheStore.clearData()
    // cc.view.setOrientation(cc.macro.ORIENTATION_PORTRAIT)
    // /**设置横屏 */
    // this.mp4.node.active = true
    view.setOrientation(macro.ORIENTATION_LANDSCAPE)
  }
  start() {
    Promise.all([this._loadingMp4(), this.loading()])
      .then((res) => {
        debugger
      })
      .catch((err) => {
        console.log(err)
      })
  }
  async loading(): Promise<boolean> {
    const sceneRet = await this._loadingScene()
    if (sceneRet) {
      const bundleRes = await ResManager.loadBundle()
      if (bundleRes) {
        const assetRes = await this._loadingAsset()
        if (assetRes) {
          return true
        }
      }
    }
    return false
  }
  /**加载场景 */
  private _loadingScene(): Promise<boolean> {
    return new Promise((resolve, reject) => {
      var self = this
      self.tips.string = '载入场景...'
      var onProgress = function (completedCount: number, totalCount: number) {
        const pressNum = Math.floor((completedCount / totalCount) * 100)
        self.percentNum.string = pressNum.toString()
      }
      var onFinished = function (err) {
        if (!err) {
          self.percentNum.string = '100%'
          return resolve(true)
        } else {
          self.tips.string = err
          return reject(false)
        }
      }
      director.preloadScene(this.loadSceneName, onProgress, onFinished)
    })
  }
  /**加载资源*/
  private async _loadingAsset(): Promise<boolean> {
    var self = this
    return new Promise((resolve, reject) => {
      self.tips.string = '载入资源...'
      var onProgress = function (completedCount: number, totalCount: number) {
        const pressNum = Math.floor((completedCount / totalCount) * 100)
        self.percentNum.string = pressNum.toString()
      }
      var onFinished = function (err) {
        if (!err) {
          self.percentNum.string = '100%'
          return resolve(true)
        } else {
          self.tips.string = err
          return reject(false)
        }
      }
      ResManager.loadDirAsset(onProgress, onFinished)
    })
  }
  //加载MP4
  private _loadingMp4(): Promise<boolean> {
    return new Promise((resolve) => {
      this.mp4.node.once('meta-loaded', () => {
        this.mp4.play()
      })
      this.mp4.node.once('completed', () => {
        this.mp4.node.active = false
        return resolve(true)
      })
    })
  }
  // /**自动登录 */
  // private async _autoLogin() {
  //   const token = CacheStore.getData(MARK.TOKEN)
  //   if (!token) {
  //     this.login_node.active = true
  //     this.tips.string = '请登录!'
  //     return
  //   }
  //   if (Config.status == STATUS.DEV) {
  //     let account: IAccount = CacheStore.getData(MARK.ACCOUNT)
  //     if (!account) {
  //       account = {
  //         account: token,
  //         realName: '张三',
  //         regTime: util.now() - 10000000,
  //         expireTime: util.now() + 99000000000,
  //         level: '普通会员',
  //         mode: VIPMODE.YouKe
  //       }
  //       CacheStore.setData(MARK.ACCOUNT, account)
  //     }
  //     GameInstance.account = account
  //     cc.director.loadScene(this.loadSceneName)
  //     return
  //   }
  //   if (Config.status == STATUS.PROD) {
  //     const account: IAccount = await Http.getAdminInfo()
  //     if (account) {
  //       GameInstance.account = account
  //       cc.director.loadScene(this.loadSceneName)
  //       return
  //     }
  //     this.login_node.active = true
  //     this.tips.string = '校验错误,请重新登录!'
  //   }
  // }
  // /**登录 */
  // public onLoginClick() {
  //   if (Config.status == STATUS.DEV) {
  //     this._testLogin()
  //     return
  //   }
  //   if (Config.status == STATUS.PROD) {
  //     this._httpLogin()
  //   }
  // }
  // /**本地测试登录 */
  // private async _testLogin() {
  //   if (!this.account_edit.string || this.account_edit.string.length < 11) {
  //     this.tips.string = '账号输入错误!'
  //     return
  //   }
  //   if (!this.password_edit.string || this.password_edit.string.length < 6) {
  //     this.tips.string = '密码输入错误!'
  //     return
  //   }
  //   CacheStore.setData(MARK.TOKEN, this.account_edit.string)
  //   const account: IAccount = {
  //     account: this.account_edit.string,
  //     realName: '张三',
  //     regTime: util.now() - 10000000,
  //     expireTime: util.now() + 99000000000,
  //     level: '普通会员',
  //     mode: VIPMODE.YouKe
  //   }
  //   GameInstance.account = account
  //   CacheStore.setData(MARK.ACCOUNT, GameInstance.account)
  //   cc.director.loadScene(this.loadSceneName)
  // }
  // /** 登录游戏*/
  // private async _httpLogin() {
  //   if (!this.account_edit.string || this.account_edit.string.length < 11) {
  //     this.tips.string = '账号输入错误!'
  //     return
  //   }
  //   if (!this.password_edit.string || this.password_edit.string.length < 6) {
  //     this.tips.string = '密码输入错误!'
  //     return
  //   }
  //   const account = await Http.adminLogin({ account: this.account_edit.string, password: this.password_edit.string })
  //   if (account) {
  //     GameInstance.account = account
  //     cc.director.loadScene(this.loadSceneName)
  //     return
  //   }
  //   this.tips.string = '效验错误,请重新登录!'
  // }
  // /**加载设置 */
  // private _loadConfig() {
  //   Config.gameTime = CacheStore.getData('gameTime') || Config.gameTime
  //   Config.gameLevel = CacheStore.getData('gameLevel') || Config.gameLevel
  //   Config.letterSize = CacheStore.getData('letterSize') || Config.letterSize
  //   Config.glassRed = CacheStore.getData('glassRed') || Config.glassRed
  //   Config.glassGreen = CacheStore.getData('glassGreen') || Config.glassGreen
  //   Config.glassBlue = CacheStore.getData('glassBlue') || Config.glassBlue
  // }
}
